﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reporter.Source_Code;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Reporter
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Process.Start("mailto:v-tigong@microsoft.com");
            OutlookTest();
        }

        static void OutlookTest()
        {

            // Create the Outlook application.
            Outlook.Application oApp = null;
            if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0)
            {
                // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                oApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
            }
            else
            {
                // If not, create a new instance of Outlook and log on to the default profile.
                oApp = new Outlook.Application();
                Outlook.NameSpace nameSpace = oApp.GetNamespace("MAPI");
                nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                //nameSpace = null;
            }

            // Create a new mail item.
            Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            // Set HTMLBody. 
            //add the body of the email
            oMsg.HTMLBody = "Hello, Jawed your message body will go here!!";
            //Add an attachment.
            String sDisplayName = "MyAttachment";
            int iPosition = (int)oMsg.Body.Length + 1;
            int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
            //now attached the file
            Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
            //Subject line
            oMsg.Subject = "Your Subject will go here.";
            // Add a recipient.
            Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
            // Change the recipient in the next line if necessary.
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("jawed.ace@gmail.com");
            oRecip.Resolve();
            // Send.
            oMsg.Send();
            // Clean up.
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oApp = null;
        }

        static void SmtpTest()
        {
            MailSender sender = new MailSender();
            sender.To.Add("v-tigong@microsoft.com");
            sender.From = new MailAddress("v-tigong@microsoft.com", "TianCai");
            sender.Subject = "Send From CSharp";
            sender.Body = "This is a test project of sending email via CSharp";
            sender.IsBodyHtml = false;
            // sender.Attachments.Add(new Attachment(@"D:\Book2.xlsx"));

            SmtpClient smtp = new SmtpClient("smtp.microsoft.com");
            // smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
            smtp.Credentials = new NetworkCredential("v-tigong@microsoft.com", "");
            smtp.EnableSsl = true;
            smtp.Timeout = 3000;

            smtp.Send(sender);
        }
    }

    public class Mailer
    {
        private SmtpClient smtpClient;
        private MailMessage mail;
        private Guid perfRunTaskID;

        /// <summary>
        /// format like(only supports one alias):  v-yulli
        /// </summary>
        private string MailFrom { get; set; }
        /// <summary>
        /// format like(only supports alias):  v-yulli;v-waltli
        /// </summary>
        private string MailTo { get; set; }

        public Mailer(Guid perfRunTaskID)
        {
            this.perfRunTaskID = perfRunTaskID;
            this.smtpClient = new SmtpClient(Utilities.GetSmtpClient());
            this.smtpClient.UseDefaultCredentials = true;

            this.mail = new MailMessage();
            this.mail.Priority = MailPriority.Normal;
            this.mail.IsBodyHtml = true;
            this.MailFrom = Utilities.GetMailFrom();
            this.MailTo = Utilities.GetMailTo();
        }

        public void Send(string perfDatas)
        {            
            string[] mailTos = this.MailTo.Split(new char[] { ';' });

            this.mail.Subject = CreateMailSubject();
            this.mail.Body = CreateMailBody(perfDatas);

            this.mail.From = new MailAddress(this.MailFrom.Trim() + "@microsoft.com");

            foreach (string mailToAddr in mailTos)
            {
                this.mail.To.Add(mailToAddr.Trim() + "@microsoft.com");
            }

           
            this.smtpClient.Send(mail);
        }

        private string CreateMailSubject()
        {
            return Utilities.GetMailSubject();
        }

        private string CreateMailBody(string perfDatas)
        {
            string reportUrl = perfDatas + "http://DailyPerfReport/Default.aspx?perfrunID=" + this.perfRunTaskID.ToString();

            string tempMailBody = "";

            return tempMailBody;
        }

    }

    class Utilities {
        public static string GetSmtpClient()
        {
            return "smtphost";
        }

        public static string GetMailTo()
        {
            return "v-tigong";
        }

        public static string GetMailFrom()
        {
            return "v-tigong";
        }

        public static string GetMailSubject()
        {
            return "Send From C#";
        }
    }

}
